package ano.it.service.impl;

import ano.it.dao.RoleDao;
import ano.it.entity.PageResult;
import ano.it.entity.QueryPageBean;
import ano.it.pojo.Permission;
import ano.it.pojo.Role;
import ano.it.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleDao roleDao;
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page=roleDao.selectByCondition(queryPageBean.getQueryString());
        long total=page.getTotal();
        List<Permission> rows=page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void addPermission(Integer[] permissionIds, Integer roleId) {
        roleDao.deleteRolePermission(roleId);

        for (Integer permissionId : permissionIds) {
            Map<String,Integer> map=new HashMap<>();
            map.put("permissionId",permissionId);
            map.put("roleId",roleId);
            roleDao.addPermission(map);
        }
    }

    @Override
    public List<Integer> findPermissionIdsByUserId(Integer id) {
        return roleDao.findPermissionIdsByUserId(id);
    }
}
